package com.albaba.designpattern.flyweight.text_002;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 享元工厂-生产五子棋棋子
 * @Author zx
 * @Date 2025/2/21 17:16
 * @Version 1.0
 */
public class GobangFactory {


    // 享元池
    private static Map<String, GobangFlyweight> pool;

    // 创建共享享元对象
    public GobangFactory() {
        pool = new HashMap<>();
        GobangFlyweight black = new BlackGobang();
        GobangFlyweight white = new WriteGobang();

        pool.put("b", black);
        pool.put("w", white);
    }

    // 获取唯一享元工厂对象的方法
    public static GobangFactory getInstance() {
        return SingletonHolder.instance;
    }

    // 静态内部类
    private static class SingletonHolder {
        // 静态初始化器，由JVM来保证线程安全
        private static final GobangFactory instance = new GobangFactory();
    }


    // 获取集合中的享元对象
    public GobangFlyweight getGobang(String color) {
        return pool.get(color);
    }



}
